﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_admin_save_address : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string hash = WebhelperMethods.URLHash(Request);
            if (string.IsNullOrWhiteSpace(hash))
            {
                Response.Redirect("login.aspx");
                return;
            }

            PomplingCustomers customer = PomplingCustomers.CustomerSelect(hash);
            if (string.IsNullOrWhiteSpace(customer.PomplingCustomerID))
            {
                Response.Redirect("login.aspx");
                return;
            }

            // fill the customer address combo
            List<PomplingProperty> tempAddressList = PomplingProperty.SelectCustomerAddressList(hash);

            CmbAddressList.DataTextField = "AddressLine1";
            CmbAddressList.DataValueField = "PomplingAddressID";
            PomplingProperty blankProperty = new PomplingProperty();
            blankProperty.PomplingAddressID = -1;
            blankProperty.AddressLine1 = string.Empty;
            List<PomplingProperty> addressList = new List<PomplingProperty>();

            addressList.Add(blankProperty);
            addressList.AddRange(tempAddressList);
            CmbAddressList.DataSource = addressList;
            CmbAddressList.DataBind();
            CmbAddressList.SelectedValue = "-1";

            //string sAddressId = WebhelperMethods.GetUrlParamValue(Globals.URL_VAR_ADDRESS_ID, Request);
            string sAddressID = WebhelperMethods.GetUrlParamValue(Globals.URL_VAR_ADDRESS_ID, Request);

            // if the user wants to update an address out of the combo
            if (!string.IsNullOrEmpty(sAddressID))
            {
                int addressID;
                Int32.TryParse(sAddressID, out addressID);
                CmbAddressList.SelectedValue = addressID.ToString();
                PomplingProperty address = PomplingProperty.SelectAddress(addressID);

                TxtAddressLine1.Text = address.AddressLine1;
                TxtAddressLine2.Text = address.AddressLine2;
                TxtCity.Text = address.City;
                TxtCounty.Text = address.County;
                TxtPostcode.Text = address.Postcode;
            }
            else
            {
                BtnRemoveFromList.Enabled = false;
            }
        }
    }

    protected void CmbAddressList_TextChanged(object sender, EventArgs e)
    {
        string sAddressID = CmbAddressList.SelectedValue;
        int addressID = -1;
        Int32.TryParse(sAddressID, out addressID);
        string companyID = WebhelperMethods.GetUrlParamValue("c", Request);
        if (addressID <= 0)
        {
            TxtAddressLine1.Text = string.Empty;
            TxtAddressLine2.Text = string.Empty;
            TxtCity.Text = string.Empty;
            TxtCounty.Text = string.Empty;
            TxtPostcode.Text = string.Empty;

            Response.Redirect("redirect.aspx?c=" + companyID + "&a=&pagefrom=admin_save_address");
            return;
        }

        PomplingProperty property = PomplingProperty.SelectAddress(addressID);
        //int webPropertyID = property.WebPropertyID;
        Response.Redirect("redirect.aspx?c=" + companyID + "&a=" + CmbAddressList.SelectedValue + "&pagefrom=admin_save_address");
    }
    protected void SaveAddress_Click(object sender, EventArgs e)
    {
        if (TxtAddressLine1.Text.Trim().Length == 0 && TxtPostcode.Text.Trim().Length == 0)
        {
            LblError.Text = "Please supply a minimum of Address Line1.";
            return;
        }

        PomplingProperty p = new PomplingProperty();

        // if this is an existing property get the id from the url
        //string sWebPropertyID = WebhelperMethods.GetUrlParamValue(Globals.URL_VAR_WEB_PROPERTY_ID, Request);
        string sAddressId = WebhelperMethods.GetUrlParamValue(Globals.URL_VAR_ADDRESS_ID, Request);

        if (!string.IsNullOrEmpty(sAddressId))
        {
            int addressID;
            if (Int32.TryParse(sAddressId, out addressID))
                p.PomplingAddressID = addressID;
        }
        p.AddressLine1 = TxtAddressLine1.Text.Trim();
        p.AddressLine2 = TxtAddressLine2.Text.Trim();
        p.City = TxtCity.Text.Trim();
        p.County = TxtCounty.Text.ToUpper();
        p.DateAdded = DateTime.Now;
        p.Postcode = TxtPostcode.Text.Trim().ToUpper();
        
        bool saved = p.SaveAddress(Request["c"]);
        if (p.PomplingAddressID > 0)
        {
            // note changed from save_property_let_info.asp ...
            string newUri = "admin_save_property_let_info.aspx" + Globals.BuildAdminURLParams(Request, p.PomplingAddressID.ToString());
            Response.Redirect(newUri);
        }
    }
    protected void BtnRemoveFromList_Click(object sender, EventArgs e)
    {
        // post back to the server, go to another page then back here so all reloads
        string sAddressID = CmbAddressList.SelectedValue;
        int addressID = -1;
        Int32.TryParse(sAddressID, out addressID);
        string customerID = Request["c"];
        if (addressID >= 0)
        {
            PomplingProperty.Delete(addressID, customerID);
        }

        Response.Redirect("redirect.aspx?c=" + customerID + "&=&pagefrom=admin_save_address");
    }
    //protected void Next_Click(object sender, EventArgs e)
    //{
    //    string newUri = "admin_save_property_let_info.aspx" + Globals.BuildAdminURLParams(Request, WebhelperMethods.GetUrlParamValue(Globals.URL_VAR_ADDRESS_ID, Request));
    //    Response.Redirect(newUri);
    //}
}